import Request from './request'
import {
	comboParameter
} from '@/common/util'

const test = new Request()

test.setConfig((config) => { /* 设置全局配置 */
	config.baseUrl = 'https://api.yfkjqhw.com/'
	config.header = {
		'Content-Type': 'application/json;charset=UTF-8'
	};
	config.dataType = 'json'
	config.responseType = 'text'
	return config
})

test.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
	const token = uni.getStorageSync("token");
	if (token) {
		config.header['Authorization'] = `Bearer ${token}`
	}
	if (config.method === 'get' || config.method === 'GET') {
		if (config.params) {
			config.params = comboParameter(config.params);
		}
	}
	if (config.method === 'post' || config.method === 'POST' || config.method === 'put' || config.method === 'PUT' ||
		config.method === 'DELETE' || config.method === 'delete') {
		if (config.data) {
			if (config.header['Content-Type'] == "multipart/form-data") {
				config.data = config.data;
			} else {
				config.data = comboParameter(config.data);
			}
		}
	}
	if (config.method === 'upload' || config.method === 'UPLOAD') {
		if (config.formData) {
			config.formData = comboParameter(config.formData);
		}
	}
	console.log(config)
	return config
})

test.interceptor.response((response) => { /* 请求之后拦截器 */
	return response
}, (response) => { // 请求错误做点什么
	return response
})

const http = new Request()
// 获取token, 意见反馈就请求111的
http.setConfig((config) => { /* 设置全局配置 */
	// config.baseUrl = 'https://app.yfkjqhw.com/app/' /* 根域名不同 */
	// config.baseUrl = 'http://192.168.0.115:8080/' /* 测试域名不同 */
	// config.baseUrl = 'http://192.168.0.117:8080/' /* 测试域名不同 */
	config.baseUrl = 'https://api.yfkjqhw.com/api/' /* 根域名不同 */
	config.header = {
		'Content-Type': 'application/json;charset=UTF-8'
	};
	config.dataType = 'json'
	config.responseType = 'text'
	return config
})

http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
	const token = uni.getStorageSync("token");
	if (token) {
		config.header['Authorization'] = `Bearer ${token}`
	}
	if (config.method === 'get' || config.method === 'GET') {
		if (config.params) {
			config.params = comboParameter(config.params);
		}
	}
	if (config.method === 'post' || config.method === 'POST' || config.method === 'put' || config.method === 'PUT' ||
		config.method === 'DELETE' || config.method === 'delete') {
		if (config.data) {
			if (config.header['Content-Type'] == "multipart/form-data") {
				config.data = config.data;
			} else {
				config.data = comboParameter(config.data);
			}
		}
	}
	if (config.method === 'upload' || config.method === 'UPLOAD') {
		if (config.formData) {
			config.formData = comboParameter(config.formData);
		}
	}
	return config
})


http.interceptor.response((response) => { /* 请求之后拦截器 */
	// if (response.data.code == 401 || response.data.code == 403 || response.data.code == 440) {
	// 	this.$Router.push({
	// 		name: 'wxLogin'
	// 	});
	// 	return;
	// }
	return response
}, (response) => { // 请求错误做点什么
	// console.log(response);
	return response
})

export {
	http,
	test
}
